(() => {

    'use strict';
    goog.provide('Blockly.Arduino.ruilong');
    goog.require('Blockly.Arduino');

   Blockly.Arduino.forBlock['ruilong_robot_button'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var code = "";
        if (window.isNaN(dropdown_pin)) {
            var funcName = 'mixly_digitalRead';
            var code2 = 'int' + ' ' + funcName + '(uint8_t pin) {\n'
                + '  pinMode(pin, INPUT);\n'
                + '  return digitalRead(pin);\n'
                + '}\n';
            Blockly.Arduino.definitions_[funcName] = code2;
            code = 'mixly_digitalRead(' + dropdown_pin + ')';
        } else {
            if (Blockly.Arduino.setups_['setup_output_' + dropdown_pin]) {
                //存在pinMode已设为output则不再设为input
            } else {
                Blockly.Arduino.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
            }
            code = 'digitalRead(' + dropdown_pin + ')';
        }
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };


  
    Blockly.Arduino.forBlock.ruilong_robot_blink_read = Blockly.Arduino.forBlock.ruilong_robot_button;

       //shadow块C代码
    Blockly.Arduino.forBlock['ruilong_robot_pins_digital'] = function () {
        var code = this.getFieldValue('PIN');
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };
    Blockly.Arduino.forBlock.ruilong_robot_linefinder_pins_analog = Blockly.Arduino.forBlock.ruilong_robot_pins_digital;
    Blockly.Arduino.forBlock.ruilong_robot_sound_pins_analog = Blockly.Arduino.forBlock.ruilong_robot_pins_digital;
    Blockly.Arduino.forBlock.ruilong_robot_button_pins_digital = Blockly.Arduino.forBlock.ruilong_robot_pins_digital;
    Blockly.Arduino.forBlock.ruilong_robot_pins_ir_recv = Blockly.Arduino.forBlock.ruilong_robot_pins_digital;
    Blockly.Arduino.forBlock.ruilong_robot_pins_blink = Blockly.Arduino.forBlock.ruilong_robot_pins_digital;
    Blockly.Arduino.forBlock.ruilong_robot_pins_buzzer = Blockly.Arduino.forBlock.ruilong_robot_pins_digital;
    Blockly.Arduino.forBlock.ruilong_robot_pins_led = Blockly.Arduino.forBlock.ruilong_robot_pins_digital;
    Blockly.Arduino.forBlock.ruilong_robot_inout_onoff = Blockly.Arduino.forBlock.ruilong_robot_pins_digital;


    Blockly.Arduino.forBlock['ruilong_robot_hc_sr04_trig'] = function () {
        var code = this.getFieldValue('PIN1');
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock['ruilong_robot_hc_sr04_echo'] = function () {
        var code = this.getFieldValue('PIN2');
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock['ruilong_robot_pins_8833motor'] = function () {
        var code = this.getFieldValue('ID');
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock['ruilong_robot_pins_I2Cmotor'] = function () {
        var code = this.getFieldValue('ID');
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock['ruilong_robot_inout_onoff'] = function () {
        var code = this.getFieldValue('BL');
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock['ruilong_robot_blink'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var dropdown_stat = Blockly.Arduino.valueToCode(this, 'BL', Blockly.Arduino.ORDER_ATOMIC);
        var code = "";
        if (window.isNaN(dropdown_pin)) {
            code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
        } else {
            Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
        }
        code += 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
        return code;
    };

    // 

    Blockly.Arduino.forBlock['ruilong_robot_linefinder'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var code = 'analogRead(' + dropdown_pin + ')';
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };
    Blockly.Arduino.forBlock.ruilong_robot_sound = Blockly.Arduino.forBlock.ruilong_robot_linefinder;

    //超声波测距
    Blockly.Arduino.forBlock['ruilong_robot_hc_sr04'] = function () {
        var dropdown_pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
        var dropdown_pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';
        Blockly.Arduino.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', INPUT);\n';
        var funcName = 'checkdistance_' + dropdown_pin1 + '_' + dropdown_pin2;
        var code = 'float' + ' ' + funcName + '() {\n'
            + '  digitalWrite(' + dropdown_pin1 + ', LOW);\n' + '  delayMicroseconds(2);\n'
            + '  digitalWrite(' + dropdown_pin1 + ', HIGH);\n' + '  delayMicroseconds(10);\n'
            + '  digitalWrite(' + dropdown_pin1 + ', LOW);\n'
            + '  float distance = pulseIn(' + dropdown_pin2 + ', HIGH) / 58.00;\n'
            + '  delay(10);\n' + '  return distance;\n'
            + '}\n';
        Blockly.Arduino.definitions_[funcName] = code;
        return [funcName + '()', Blockly.Arduino.ORDER_ATOMIC];
    }



    //红外接收
    Blockly.Arduino.forBlock['ruilong_robot_ir_recv'] = function () {
        var variable = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
        Blockly.Arduino.definitions_['var_declare' + variable] = 'long ' + variable + ';';
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var branch = Blockly.Arduino.statementToCode(this, 'DO');
        var branch2 = Blockly.Arduino.statementToCode(this, 'DO2');
        var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
        Blockly.Arduino.definitions_['include_IRremote'] = '#include <IRremote.h>\n';
        //Blockly.Arduino.definitions_['var_declare'+varName] = 'long '+varName+';\n';
        Blockly.Arduino.definitions_['var_ir_recv' + dropdown_pin] = 'IRrecv irrecv_' + dropdown_pin + '(' + dropdown_pin + ');\ndecode_results results_' + dropdown_pin + ';\n';
        Blockly.Arduino.setups_['setup_ir_recv_' + dropdown_pin] = 'irrecv_' + dropdown_pin + '.enableIRIn();';
        var code = "if (irrecv_" + dropdown_pin + ".decode(&results_" + dropdown_pin + ")) {\n"
        code += '  ' + variable + '=results_' + dropdown_pin + '.value;\n';
        code += '  String type="UNKNOWN";\n';
        ////////////////////////////////////////////////////////////////
        code += '  String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};\n';
        code += '  if(results_' + dropdown_pin + '.decode_type>=1&&results_' + dropdown_pin + '.decode_type<=13){\n';
        code += '    type=typelist[results_' + dropdown_pin + '.decode_type];\n'
        code += '  }\n';
        code += '  Serial.print("IR TYPE:"+type+"  ");\n';
        code += branch;
        code += '  irrecv_' + dropdown_pin + '.resume();\n'
        code += '} else {\n';
        code += branch2;
        code += '}\n';
        return code;
    };
    //机器人-电机转动
    Blockly.Arduino.forBlock['ruilong_robot_8833motor'] = function () {
        var id = Blockly.Arduino.valueToCode(this, 'ID', Blockly.Arduino.ORDER_ATOMIC);
        var speed = Blockly.Arduino.valueToCode(this, 'SPEED', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
        var code = 'motor(' + id + ',' + speed + ');\n';
        Blockly.Arduino.setups_['setup_output_4'] = 'pinMode(4, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_5'] = 'pinMode(5, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_6'] = 'pinMode(6, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_7'] = 'pinMode(7, OUTPUT);';


        var funcName = 'motor';
        var code2;
        code2 = 'void motor(int id,int speed){\n';
        code2 += 'int pin_s,pin_d;\n '
        code2 += 'switch(id){\n';
        code2 += 'case 1:pin_d=4;pin_s=5;break;\n';
        code2 += 'case 2:pin_d=7;pin_s=6;break;\n';
        code2 += 'default:break;}\n';
        code2 += 'if(speed==0){digitalWrite(pin_d,0);digitalWrite(pin_s,0);}\n';
        code2 += 'if(speed>0){digitalWrite(pin_d,0);analogWrite(pin_s,speed);}\n';
        code2 += '       else{digitalWrite(pin_d,1);analogWrite(pin_s,(255+speed));}\n';
        code2 += '}\n';

        Blockly.Arduino.definitions_[funcName] = code2;
        return code;
    };
    Blockly.Arduino.forBlock['ruilong_robot_8833motor2'] = function () {
        //var id = Blockly.Arduino.valueToCode(this, 'ID', Blockly.Arduino.ORDER_ATOMIC);
        var speed = Blockly.Arduino.valueToCode(this, 'SPEED', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
        var code = 'motor2(' + speed + ');\n';
        Blockly.Arduino.setups_['setup_output_4'] = 'pinMode(4, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_5'] = 'pinMode(5, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_6'] = 'pinMode(6, OUTPUT);';
        Blockly.Arduino.setups_['setup_output_7'] = 'pinMode(7, OUTPUT);';


        var funcName = 'motor2';
        var code2;
        code2 = 'void motor2(int speed){\n';
        code2 += 'if(speed==0){digitalWrite(4,0);digitalWrite(5,0);digitalWrite(7,0);digitalWrite(6,0);}\n';
        code2 += 'if(speed>0){digitalWrite(4,0);analogWrite(5,speed);digitalWrite(7,0);analogWrite(6,speed);}\n';
        code2 += '       else{digitalWrite(4,1);analogWrite(5,(255+speed));digitalWrite(7,1);analogWrite(6,(255+speed));}\n';
        code2 += '}\n';

        Blockly.Arduino.definitions_[funcName] = code2;
        return code;
    };

    //执行器-蜂鸣器频率选择列表
    Blockly.Arduino.forBlock['ruilong_robot_tone_notes'] = function () {
        var code = this.getFieldValue('STAT');
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    //执行器-蜂鸣器
    Blockly.Arduino.forBlock['ruilong_robot_tone'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var fre = Blockly.Arduino.valueToCode(this, 'FREQUENCY', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
        var dur = Blockly.Arduino.valueToCode(this, 'DURATION', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
        Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
        Blockly.Arduino.definitions_['newtone0'] = 'float period;\nfloat pulse;';
        Blockly.Arduino.definitions_['newtone1'] = 'void newtone(int tonePin, int frequency, int duration)';
        Blockly.Arduino.definitions_['newtone2'] = '{\nfloat period = 1000000.0 /frequency;';
        Blockly.Arduino.definitions_['newtone3'] = 'float pulse = period / 2.0;';
        Blockly.Arduino.definitions_['newtone4'] = 'for (int i=1; i<=((duration * 1000.0)/period);i=i+1)';
        Blockly.Arduino.definitions_['newtone5'] = ' {\npinMode(tonePin, OUTPUT);\n digitalWrite(tonePin,HIGH);';
        Blockly.Arduino.definitions_['newtone6'] = 'delayMicroseconds(pulse);';
        Blockly.Arduino.definitions_['newtone7'] = 'pinMode(tonePin, OUTPUT);\n digitalWrite(tonePin,LOW);';
        Blockly.Arduino.definitions_['newtone8'] = ' delayMicroseconds(pulse);\n}\n}\n';
        var code = "newtone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
        return code;
    };

    //执行器-蜂鸣器结束声音
    Blockly.Arduino.forBlock['ruilong_robot_newNoTone'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var Delay_time = Blockly.Arduino.valueToCode(this, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
        var code = "digitalWrite(" + dropdown_pin + ", LOW);\n";
        code += 'delay(' + Delay_time + ');';
        return code;
    };

    Blockly.Arduino.forBlock.ruilong_speaker_tone = Blockly.Arduino.forBlock.ruilong_robot_tone;
    Blockly.Arduino.forBlock.ruilong_speaker_Notone = Blockly.Arduino.forBlock.ruilong_robot_newNoTone;

    Blockly.Arduino.forBlock['ruilong_robot_rgb_init'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var value_ledcount = Blockly.Arduino.valueToCode(this, 'LEDCOUNT', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(' + value_ledcount + ');';
        Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
        Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
        return '';
    };

    Blockly.Arduino.forBlock['ruilong_robot_rgb_brightness'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        var code = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_robot_rgb'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var value_led = Blockly.Arduino.valueToCode(this, '_LED_', Blockly.Arduino.ORDER_ATOMIC);
        var value_rvalue = Blockly.Arduino.valueToCode(this, 'RVALUE', Blockly.Arduino.ORDER_ATOMIC);
        var value_gvalue = Blockly.Arduino.valueToCode(this, 'GVALUE', Blockly.Arduino.ORDER_ATOMIC);
        var value_bvalue = Blockly.Arduino.valueToCode(this, 'BVALUE', Blockly.Arduino.ORDER_ATOMIC);
        //Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(12);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
        }

        var code = 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(' + value_led + ', ' + value_rvalue + ',' + value_gvalue + ',' + value_bvalue + ');\n';
        code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_robot_rgb2'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var value_led = Blockly.Arduino.valueToCode(this, '_LED_', Blockly.Arduino.ORDER_ATOMIC);
        var color = goog.color.hexToRgb(this.getFieldValue('RGB_LED_COLOR'));
        //Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(12);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
        }
        var code = 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(' + value_led + ', ' + color + ');\n';
        code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_robot_rgb3'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var wait_time = Blockly.Arduino.valueToCode(this, 'WAIT', Blockly.Arduino.ORDER_ATOMIC);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(12);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
            Blockly.Arduino.setups_['setup_rgb_display_setBrightness' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        }
        var funcName2 = 'Wheel';
        var code2 = 'uint32_t Wheel(byte WheelPos) {\n';
        code2 += 'if(WheelPos < 85) {return rgb_display_' + dropdown_rgbpin + '.Color(WheelPos * 3, 255 - WheelPos * 3, 0);} \n';
        code2 += 'else if(WheelPos < 170) {WheelPos -= 85; return rgb_display_' + dropdown_rgbpin + '.Color(255 - WheelPos * 3, 0, WheelPos * 3);}\n ';
        code2 += 'else {WheelPos -= 170;return rgb_display_' + dropdown_rgbpin + '.Color(0, WheelPos * 3, 255 - WheelPos * 3);}\n';
        code2 += '}\n';
        Blockly.Arduino.definitions_[funcName2] = code2;

        var funcName3 = 'rainbow';
        var code3 = 'void rainbow(uint8_t wait) { uint16_t i, j;\n';
        code3 += '      for(j=0; j<256; j++) {               \n';
        code3 += '        for(i=0; i<rgb_display_' + dropdown_rgbpin + '.numPixels(); i++) {\n';
        code3 += '         rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, Wheel((i+j) & 255));}\n';
        code3 += '      rgb_display_' + dropdown_rgbpin + '.show();\n';
        code3 += '      delay(wait);}}\n';

        Blockly.Arduino.definitions_[funcName3] = code3;

        var code = 'rainbow(' + wait_time + ');\n'
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_robot_rgb4'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var wait_time = Blockly.Arduino.valueToCode(this, 'WAIT', Blockly.Arduino.ORDER_ATOMIC);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(12);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
            Blockly.Arduino.setups_['setup_rgb_display_setBrightness' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        }
        var funcName2 = 'Wheel';
        var code2 = 'uint32_t Wheel(byte WheelPos) {\n';
        code2 += 'if(WheelPos < 85) {return rgb_display_' + dropdown_rgbpin + '.Color(WheelPos * 3, 255 - WheelPos * 3, 0);} \n';
        code2 += 'else if(WheelPos < 170) {WheelPos -= 85; return rgb_display_' + dropdown_rgbpin + '.Color(255 - WheelPos * 3, 0, WheelPos * 3);}\n ';
        code2 += 'else {WheelPos -= 170;return rgb_display_' + dropdown_rgbpin + '.Color(0, WheelPos * 3, 255 - WheelPos * 3);}\n';
        code2 += '}\n';
        Blockly.Arduino.definitions_[funcName2] = code2;

        var funcName3 = 'rainbow';
        var code3 = 'void rainbow(uint8_t wait) { uint16_t i, j;\n';
        code3 += '      for(j=0; j<256; j++) {               \n';
        code3 += '        for(i=0; i<rgb_display_' + dropdown_rgbpin + '.numPixels(); i++) {\n';
        code3 += '         rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, Wheel((i+j) & 255));}\n';
        code3 += '      rgb_display_' + dropdown_rgbpin + '.show();\n';
        code3 += '      delay(wait);}}\n';

        Blockly.Arduino.definitions_[funcName3] = code3;

        var funcName4 = 'rainbowCycle';
        var code4 = 'void rainbowCycle(uint8_t wait) {uint16_t i, j;\n';
        code4 += '    for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel\n';
        code4 += '      for(i=0; i< rgb_display_' + dropdown_rgbpin + '.numPixels(); i++) {\n';
        code4 += '        rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, Wheel(((i * 256 / rgb_display_' + dropdown_rgbpin + '.numPixels()) + j) & 255));}\n';
        code4 += '     rgb_display_' + dropdown_rgbpin + '.show();\n';
        code4 += '     delay(wait);}}\n';

        Blockly.Arduino.definitions_[funcName4] = code4;

        var code = 'rainbowCycle(' + wait_time + ');\n'
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_robot_tm1650_power'] = function () {
        var stat = this.getFieldValue("STAT");
        Blockly.Arduino.definitions_['dinclude_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_TM1650'] = '#include <TM1650.h>';
        Blockly.Arduino.definitions_['var_ruilong_4display'] = 'TM1650 ruilong_4display;';
        Blockly.Arduino.setups_['setup_wire_begin'] = 'Wire.begin();';
        Blockly.Arduino.setups_['setup_ruilong_4display_init'] = 'ruilong_4display.init();';
        return 'ruilong_4display.' + stat + '();\n';
    };
    Blockly.Arduino.forBlock['ruilong_robot_tm1650_displayString'] = function () {
        var value = Blockly.Arduino.valueToCode(this, 'VALUE', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['dinclude_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_TM1650'] = '#include <TM1650.h>';
        Blockly.Arduino.definitions_['var_ruilong_4display'] = 'TM1650 ruilong_4display;';
        Blockly.Arduino.setups_['setup_wire_begin'] = 'Wire.begin();';
        Blockly.Arduino.setups_['setup_ruilong_4display_init'] = 'ruilong_4display.init();';
        return 'ruilong_4display.displayString(' + value + ');\n';
    };
    Blockly.Arduino.forBlock['ruilong_robot_tm1650_showDot'] = function () {
        var no = this.getFieldValue("NO");
        var stat = this.getFieldValue("STAT");
        Blockly.Arduino.definitions_['dinclude_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_TM1650'] = '#include <TM1650.h>';
        Blockly.Arduino.definitions_['var_ruilong_4display'] = 'TM1650 ruilong_4display;';
        Blockly.Arduino.setups_['setup_wire_begin'] = 'Wire.begin();';
        Blockly.Arduino.setups_['setup_ruilong_4display_init'] = 'ruilong_4display.init();';
        return 'ruilong_4display.setDot(' + no + ',' + stat + ');\n';
    };

    })();